package com.drinks.yiniang.ui.activity.message;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.View;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.viewpager.TabFragmentAdapter;
import com.drinks.yiniang.common.listener.OnSetMessageReadListener;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.io.api.MessageApiIO;
import com.drinks.yiniang.ui.fragment.message.CommonMessageFragment;
import com.drinks.yiniang.ui.fragment.message.NoticeMessageFragment;
import com.drinks.yiniang.ui.fragment.message.GoodsMessageFragment;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.fragment.BaseLazyFragment;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 消息页面
 * @author han
 * @date 2019/09/08
 */
public class MessageActivity extends BaseTranBarActivity implements OnNavgationBarClickListener,
        TabLayout.OnTabSelectedListener, OnSetMessageReadListener {

    private final String ID_CLEAD_ALL_MSG = "-1";

    //nav
    private NavigationBarLayout nav_bar_message;
    private TabLayout tab_message;
    private ViewPager vp_message;

    private GoodsMessageFragment mGoodsMessageFragment;
    private NoticeMessageFragment mCommunityMessageFragment;
    private CommonMessageFragment mCommonMessageFragment;

    private TabFragmentAdapter mTabFragmentAdapter;

    private List<BaseLazyFragment> mFragments;
    private String[] mTabs;

    @Override
    protected void initView() {
        nav_bar_message = findViewById(R.id.nav_bar_message);

        tab_message = findViewById(R.id.tab_message);

        vp_message = findViewById(R.id.vp_message);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        initTab();
        initViewPager();
        getTotalUnReadMsg();
    }

    private void initTab() {
        mTabs = new String[]{getString(R.string.text_goods), getString(R.string.text_notice), getString(R.string.text_message)};

        for(int i = 0; i < mTabs.length; i++) {
            tab_message.addTab(tab_message.newTab().setText(mTabs[i]));
        }
    }

    //初始化viewpager数据
    private void initViewPager() {
        mFragments = new ArrayList<>(mTabs.length);

        mGoodsMessageFragment = GoodsMessageFragment.newInstance();
        mCommunityMessageFragment = NoticeMessageFragment.newInstance();
        mCommonMessageFragment = CommonMessageFragment.newInstance();

        mFragments.add(mGoodsMessageFragment);
        mFragments.add(mCommunityMessageFragment);
        mFragments.add(mCommonMessageFragment);

        mTabFragmentAdapter = new TabFragmentAdapter(getSupportFragmentManager(), mFragments, mTabs);

        vp_message.setOffscreenPageLimit(2);
        vp_message.setAdapter(mTabFragmentAdapter);
        tab_message.setupWithViewPager(vp_message);
    }

    @Override
    protected void initListener() {
        nav_bar_message.setOnNavgationBarClickListener(this);
        tab_message.addOnTabSelectedListener(this);
    }

    @Override
    public void widgetClick(View v) {

    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_message;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {
        setMessageRead(ID_CLEAD_ALL_MSG, true);
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onSetMessageRead(String msgType, String msgId, String isRead) {
        if (StringUtils.isEmpty(msgId) || Constants.STATUS_GLOBAL_YES.equals(isRead)) {//已读
            return;
        }

        setMessageRead(msgId, false);
    }

    //设置消息为全部已读
    private void setMessageRead(String msgId, boolean isShowLoading) {
        if(isShowLoading) {
            showLoadingDialog();
        }

        MessageApiIO.getInstance().setMessageRead(msgId, new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                nav_bar_message.setRedDotText(res.getData());
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }


    //获取未读消息总数
    private void getTotalUnReadMsg() {
        MessageApiIO.getInstance().getTotalUnReadMessag(new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                nav_bar_message.setRedDotText(res.getData());
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {

            }

            @Override
            public void onComplete() {

            }
        });
    }
}
